﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;
using PH.Model.Results;
using PH.Model.Webs;
using PH.Logic.Webs;
namespace PH.Web.Management.Areas.Systems.Controllers
{
    [RouteArea("Systems")]
    [RoutePrefix("Web")]
    [Authorize(Roles = "WebAdmin")]
    public class WebApiController : ApiController
    {
        [Route("GetPages")]
        [HttpPost]
        public JsonEntitySet<PageModel> GetPages(JObject request)
        {
            string errinfo = "no data";
            try
            {
                PageQuery sea = new PageQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                var result = PageLogic.Gets(sea);
                return new JsonEntitySet<PageModel>(result);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<PageModel>(null, errinfo, false);
        }

        [Route("SavePage")]
        [HttpPost]
        public JsonResult<bool> SavePage(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                #region 收集参数
                PageModel page = new PageModel();
                page.ID = request.Value<int>("id");
                page.CaseID = request.Value<int>("caseID");
                page.Title = request.Value<string>("title");
                page.Description = request.Value<string>("description");
                page.PageType = request.Value<int>("pageType");
                page.Visible = true;

                #endregion

                if (page.ID == 0)
                    PageLogic.CreateNewPage(page);
                else
                    PageLogic.UpdatePage(page);

                return new JsonResult<bool>(true);
            }
            catch (Utils.PhException ex){
                errinfo = ex.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

        [Route("DeletePage")]
        [HttpPost]
        public JsonResult<bool> DeletePage(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                #region 收集参数
                var id = request.Value<int>("id");
                #endregion
                
                PageLogic.DeletePage(id);
                return new JsonResult<bool>(true);
            }
            catch (Utils.PhException ex)
            {
                errinfo = ex.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
    }
}
